package com.qingxin.wuye.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyGarden;
import com.qingxin.wuye.entity.WyPropertyCompany;
import com.qingxin.wuye.service.WyGardenService;
import com.qingxin.wuye.service.WyPropertyCompanyService;

@Controller
@RequestMapping("/garden")
public class GardenAction extends BaseAction {

	@Autowired
	private WyGardenService gardenService;
	@Autowired
	private WyPropertyCompanyService companyService;
	
	
	/**
	 * 获取表单数据
	 * @param companyid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getGardenData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getGardenData(String companyid, HttpServletRequest request){
		return gardenService.getUITreeData(companyid, getLoginUser(request), false);
	}
	
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(SystemCfg.isAdminUser(loginUser)){
			return "wuye/garden/garden_list_admin";
		}
		return "wuye/garden/garden_list";
	}
	
	
	/**
	 * 获取列表数据
	 * @param datatype
	 * @param leftdata
	 * @param gardenName
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String leftdata, String gardenName, PageParam pageParam, HttpServletRequest request){
		String companyid = "";
		if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			companyid = leftdata;
		}
		return gardenService.list(companyid, gardenName, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加信息
	 * @param model
	 * @param datatype
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String datatype, String leftdata, HttpServletRequest request){
		String companyid = "";
		if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			companyid = leftdata;
		}
		LoginUser loginUser = getLoginUser(request);
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		model.put("companyid", companyid);
		
		return "wuye/garden/garden_add";
	}
	
	
	/**
	 * 保存
	 * @param garden
	 * @param result
	 * @param companyid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid WyGarden garden, BindingResult result, String companyid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		WyPropertyCompany company = new WyPropertyCompany();
		company.setCompanyid(companyid);
		garden.setWyPropertyCompany(company);
		
		SysError info = gardenService.add(garden, getLoginUser(request), request);
		return info;
	}

	
	/**
	 * 修改信息
	 * @param model
	 * @param gardenid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String gardenid){
		WyGarden garden = gardenService.get(gardenid);
		model.put("garden", garden);
		
		if(garden != null){
			WyPropertyCompany company = companyService.get(garden.getWyPropertyCompany().getCompanyid());
			model.put("company", company);
		}
		
		return "wuye/garden/garden_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param garden
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid WyGarden garden, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = gardenService.update(garden, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 删除
	 * @param gardenid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] gardenid, HttpServletRequest request){
		if(gardenid==null || gardenid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return gardenService.delete(gardenid, getLoginUser(request), request);
	}
	
	
}
